import logging

from common.mysql.MySQLhelper import get_sql_helper
import shortuuid


sqlhelper = get_sql_helper()

# 根据95%的相似度数据，来获取到之前登录的所有用户信息
def get_wechat_user(mul_value_top, mul_value_low):
    return sqlhelper.fetch_all(
        "select id, create_time, update_time, user_nick_name, user_id, friend_nick_name, friend_nick_id, province, city, remark_name, is_group, seq, mul_value  from shequn_wechat_user where mul_value <= %s and mul_value >= %s ;",
        (mul_value_top, mul_value_low))

def get_mul_ori_by_user_id(user_id):
    return sqlhelper.fetch_all("select mul_value from shequn_wechat_user where friend_nick_id = %s",
                                (user_id))


# 更新用户信息
def update_wechat_user(nick_name, user_id, index, today, seq):
    sqlhelper.update(
        "UPDATE shequn_wechat_user SET user_nick_name = %s, user_id = %s, friend_nick_name = %s, friend_nick_id = %s, remark_name = %s, update_time = %s WHERE seq = %s; ",
        (nick_name, user_id, index.NickName, index.UserName, index.RemarkName, today, seq))

def update_wechat_user_list(insert_wechat_data):
    sql = 'UPDATE shequn_wechat_user SET user_nick_name = %s, user_id = %s, friend_nick_name = %s, friend_nick_id = %s, remark_name = %s, update_time = %s WHERE seq = %s; '
    sqlhelper.update_list(sql, insert_wechat_data)

def insert_wechat_user_list(insert_wechat_data):
    sql = 'INSERT INTO shequn_wechat_user (id, create_time, update_time, user_nick_name, user_id, friend_nick_name, friend_nick_id, province, city, remark_name, is_group, seq) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
    sqlhelper.update_list(sql, insert_wechat_data)


def update_wechat_user_by_fiend_id(friend_name, today, friend_id):
    sqlhelper.update(
        "UPDATE shequn_wechat_user SET friend_nick_name = %s, update_time = %s WHERE friend_nick_id = %s; ",
        (friend_name, today, friend_id))

#  增加用户信息
def insert_wechat_user(index, today, nick_name, user_id, seq, mul_ori):
    id = str(shortuuid.ShortUUID().random(20))
    sqlhelper.insert_one(
        "INSERT INTO shequn_wechat_user (id, create_time, update_time, user_nick_name, user_id, friend_nick_name, friend_nick_id, province, city, remark_name, is_group, seq, mul_value) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ",
        (
            id, today, today, nick_name, user_id, index.NickName, index.UserName, index.Province,
            index.City,
            index.RemarkName, False, seq, mul_ori))

def insert_new_group_wechat_user(today, user_name, user_id, friend_name, friend_id, flag, mul_ori, cmsg):
    id_wechat = str(shortuuid.ShortUUID().random(20))
    group_count = 0
    if flag:
        try:
            group_count = cmsg.group_num
        except Exception:
            group_count = 0
    sqlhelper.insert_one(
        "INSERT INTO shequn_wechat_user (id, create_time, update_time, user_nick_name, user_id, friend_nick_name, friend_nick_id, is_group, seq, mul_value, group_count) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ",
        (
            id_wechat, today, today, user_name, user_id, friend_name,
            friend_id,
            flag, 0, mul_ori, group_count))


# 增加用户信息
def insert_wechat_user_room(index, today, nick_name, user_id, seq):
    id = str(shortuuid.ShortUUID().random(20))
    sqlhelper.insert_one(
        "INSERT INTO shequn_wechat_user (id, create_time, update_time, user_nick_name, user_id, friend_nick_name, friend_nick_id, province, city, remark_name, is_group, seq) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ",
        (
            id, today, today, nick_name, user_id, index.NickName, index.UserName, index.Province,
            index.City,
            index.RemarkName, True, seq))

# 删除用户数据
def delete_wechat_user(id):
    sqlhelper.delete("DELETE from shequn_wechat_user where id = %s", (id))


def delete_wechat_user_by_user_id(user_id):
    sqlhelper.delete("DELETE from shequn_wechat_user where user_id = %s", (user_id))



def get_wechat_user_by_friend(friend_id):
    value = sqlhelper.fetch_all("select id, friend_nick_name from shequn_wechat_user where friend_nick_id = %s",
                                (friend_id))
    return value

def get_wechat_user_by_nick_id(friend_id):
    return sqlhelper.fetch_all("select friend_nick_name, remark_name, extra_name from shequn_wechat_user where friend_nick_id = %s",
                                (friend_id))